define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    confirm_url: 'order/confirm',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                // console.log(data);
                //这里我们手动设置底部的值
                $("#total").text(data.extend.total);
                $("#payment").text(data.extend.payment);
                $("#adddon").text(data.extend.adddon);
                // $("#chargeback").text(data.extend.chargeback);
            });



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                // commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), formatter: Table.api.formatter.normal, searchList: {'install': __('安装'), 'repair': __('维修')}},
                        {field: 'user.nickname', title: __('下单用户'), formatter:function(value,row,index){
                                var html='';
                                if(row.user.nickname == null){
                                    html = '平台下单';
                                }else{
                                    html = row.user.nickname;
                                }
                                return html;
                            }},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'area', title: __('省/市/区'), visible:false, searchList: function (column) {return Template('sourcetpl', {});}},
                        {field: 'region', title: __('省/市/区'),  operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, align:'left', class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'reservation_time', title: __('Reservation_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'master.nickname', title: __('接单师傅'), operate: 'LIKE'},
                        // {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'payamount', title: __('Payamount'), operate:'BETWEEN'},
                        // {field: 'paytype', title: __('Paytype'), operate: 'LIKE'},
                        {field: 'paid', title: __('支付状态'), formatter: Table.api.formatter.label, searchList: {'0': __('未付款'), '1': __('已付款')}},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'is_sure', title: __('平台确认'), formatter: Table.api.formatter.label, searchList: {'0': __('否'), '1': __('是')}},
                        {field: 'createtime', title: __('下单时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {'0': __('待接单'), '1': __('待上门'), '2': __('服务中'), '3': __('已完结'), '100': __('已取消'), '200': __('平台关闭')}},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            align:'right',
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'confirm',
                                    text: __('确认订单'),
                                    title: __('确认订单'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'order/confirm',
                                    // confirm: '确认后不可还原，确认操作？',
                                    visible: function (row) {
                                        //重点在这
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.paid == 1 && row.is_sure == 0) {
                                            return true;
                                        }

                                    },
                                    callback: function (data) {
                                        // var
                                        // console.log(JSON.stringify(data));
                                        console.log(JSON.stringify(row));
                                        // ajax保存备注信息
                                        // $.ajax({
                                        //     url: 'order/confirm',
                                        //     type: 'POST',
                                        //     dataType: 'json',
                                        //     data: {
                                        //         id: data.id,
                                        //         master_id: data.master_id
                                        //     },
                                        //     success: function (data) {
                                        //         console.log(data);
                                        //         if (data.code == 1) {
                                        //             Toastr.success(data.msg);
                                        //             $("#table").bootstrapTable('refresh',{});
                                        //         }
                                        //     }
                                        // })

                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                },
                                {
                                    name: 'allocated',
                                    text: __('指派师傅'),
                                    title: __('指派师傅'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-user-secret',
                                    url: 'master/allocated',
                                    callback: function (data) {
                                        // console.log(JSON.stringify(data));
                                        // console.log(JSON.stringify(row));
                                        // ajax保存师傅信息
                                        $.ajax({
                                            url: 'order/allocated',
                                            type: 'POST',
                                            dataType: 'json',
                                            data: {
                                                id: data.id,
                                                master_id: data.master_id
                                            },
                                            success: function (data) {
                                                console.log(data);
                                                if (data.code == 1) {
                                                    Toastr.success(data.msg);
                                                    $("#table").bootstrapTable('refresh',{});
                                                }
                                            }
                                        })

                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.master_id == 0 && row.is_sure == 1 && row.status != 100){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'transfer',
                                    text: __('转派师傅'),
                                    title: __('转派师傅'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-share-square-o',
                                    url: 'master/allocated',
                                    callback: function (data) {
                                        // console.log(JSON.stringify(data));
                                        // console.log(JSON.stringify(row));
                                        // ajax保存师傅信息
                                        $.ajax({
                                            url: 'order/transfer',
                                            type: 'POST',
                                            dataType: 'json',
                                            data: {
                                                id: data.id,
                                                master_id: data.master_id
                                            },
                                            success: function (data) {
                                                console.log(data);
                                                if (data.code == 1) {
                                                    Toastr.success(data.msg);
                                                    $("#table").bootstrapTable('refresh',{});
                                                }
                                            }
                                        })

                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.master_id != 0 && row.is_sure == 1 && row.status != 100){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: '确认退款',
                                    text: __('确认退款'),
                                    title: __('确认退款'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-close',
                                    url: 'order/cancel',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 100 && row.refund_status == 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Toastr.success('操作成功');
                                        $("#table").bootstrapTable('refresh',{});
                                        return false;
                                    }
                                },
                                {
                                    name: '取消师傅',
                                    text: __('取消师傅'),
                                    title: __('取消师傅'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-close',
                                    url: 'order/cancel_master',
                                    confirm: '确认取消该师傅接单？',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.master_id != 0 && row.status == 1){
                                            return true;
                                        }
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Toastr.success('操作成功');
                                        $("#table").bootstrapTable('refresh',{});
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            // 动态监听单选框 type 值
            $(':radio').change(function(){
                // 获取选中的值
                var type = $(this).val();

                console.log(type);
                // 执行操作
                if (type == 'install') {
                    $("#install").removeClass('hide');
                    $("#repair").addClass('hide');
                } else if (type == 'repair') {
                    $("#install").addClass('hide');
                    $("#repair").removeClass('hide');
                }
            });

            // 动态监听下拉框的值
            $("#c-package_type").on("change", function () {
                var id = $("#c-package_type").val();
                console.log(id);
                // ajax 获取数据
                $.ajax({
                    url: "package_type/get_price",
                    type: "POST",
                    dataType: "json",
                    data: {id: id},
                    success: function (data) {
                        // console.log(data);
                        // 修改文本框 amount 的值
                        $("#c-amount").val(data.data.price);
                    }
                })
            });

        },
        edit: function () {
            $(document).on("click", ".nearby", function () {
                var id, address_lng, address_lat;
                id = $("#c-id").val();
                address_lng = $("#c-lng").val();
                address_lat = $("#c-lat").val();
                
                console.log(address_lng);
                Fast.api.open('master/nearby?address_lng=' + address_lng + '&address_lat='+address_lat, __('师傅位置'),{
                    //接收传过来的参数
                    callback:function(value){
                        //在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
                        // console.log(value);
                        $.ajax({
                            url: 'order/transfer',
                            type: 'POST',
                            dataType: 'json',
                            data: {
                                id: id,
                                master_id: value.master_id
                            },
                            success: function (data) {
                                console.log(data);
                                if (data.code == 1) {
                                    Toastr.success(data.msg);
                                    $("#table").bootstrapTable('refresh',{});
                                }
                            }
                        })

                    }
                });
            });
            Controller.api.bindevent();
        },
        confirm: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        // map: function () {
        //     Controller.api.bindevent();
        // },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
